package com.phjr.jicaimaoflutter.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.phjr.jicaimaoflutter.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * description:
 * copyright  : Copyright (c) 20119
 * company    : 普华
 * date       : 2019/1/28 15:48
 * @author : austen
 */
public class WebFragment extends Fragment {
    public static final String WEB_VIEW_URL = "WBE_URL";
    @BindView(R.id.web_view)
    WebView        mWebView;
    @BindView(R.id.rl_no_data)
    RelativeLayout mRlNoData;

    public static Fragment newInstance(String url) {
        WebFragment webFragment = new WebFragment();
        Bundle bundle = new Bundle();
        bundle.putString(WEB_VIEW_URL, url);
        webFragment.setArguments(bundle);
        return webFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frag_web_view, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String webUrl = getArguments().getString(WEB_VIEW_URL, "");
        if (webUrl.equals("")) {
            mWebView.setVisibility(View.GONE);
            mRlNoData.setVisibility(View.VISIBLE);
        }

        WebSettings settings = mWebView.getSettings();
        //支持js
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.clearCache(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);

            }
        });
        mWebView.loadUrl(webUrl);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
        mWebView = null;
    }
}
